package UtilsApplication;

import java.util.ArrayList;
import java.util.List;

public class FourierUtil {

    public static Complex[] fft(double [] audio) {      

        List<Complex> newAudio = new ArrayList<Complex>();
        for(int i = 0; i < audio.length; i++) {
            newAudio.add( new Complex(audio[i],0) );
        }

        int diff = checkInputLength(audio.length);

        for (int i = 0; i < diff; i++) {
            newAudio.add(new Complex(0,0));
        }

        Complex [] audioComplex = new Complex[newAudio.size()];

        audioComplex = FFT.fft( newAudio.toArray( audioComplex) );
        
        return audioComplex;
    }

    public static double [] ifft(Complex[] arrayComplex) {

        arrayComplex = FFT.ifft( arrayComplex );

        double [] audio = new double[arrayComplex.length];

        for(int i = 0; i < arrayComplex.length; i++) {
            audio[i] = (double)arrayComplex[i].re();
        }

        return audio;
    }

    public static int checkInputLength(int length){
        int nearest = 2;
        while (nearest < length) {
            nearest *= 2;
        }
        return nearest - length;
    }

}
